import unittest
import sys, os
from os.path import join, dirname
location = join(dirname(sys.argv[0]), '..')

sys.path.insert(0, location)

import package
from package import PackageKey

class PortQueryTest(unittest.TestCase):
    def setUp(self):
        self.d = {
            PackageKey.NAME                 : 'python25',
            PackageKey.REVISION             : '1',
            PackageKey.VERSION              : '2.5',
            PackageKey.DIRECTORY            : 'lang/python25',
            PackageKey.HOMEPAGE             : 'http://www.python.org/',
            PackageKey.VARIANTS             : ['darwin_8'],
            PackageKey.DESCRIPTION          : 'Python is an interpreted, interactive, object-oriented programming language.',
            PackageKey.BUILD_DEPENDENCIES   : [],
            PackageKey.LIBRARY_DEPENDENCIES : [],
            PackageKey.RUNTIME_DEPENDENCIES : [],
            PackageKey.PLATFORMS            : ['darwin'],
            PackageKey.MAINTAINERS           : ['mww@macports.org', 'pmq@macports.org'] 
        }
    
    def testContructors(self):
        p1 = package.Package(self.d)
        for k in package.PackageKey:
            self.assertEquals(p1[k], self.d.get(k))
            self.assertEquals(getattr(p1, k), self.d[k])

        self.assertRaises(KeyError, p1.__getitem__, 'invalid')
        
        p2 = package.Package(
            name                   = 'python25',
            revision               = '1',
            version                = '2.5',
            directory              = 'lang/python25',
            homepage               = 'http://www.python.org/',
            variants               = ['darwin_8'],
            description            = 'Python is an interpreted, interactive, object-oriented programming language.',
            build_dependencies     = [],
            library_dependencies   = [],
            runtime_dependencies   = [],
            platforms              = ['darwin'],
            maintainers             = ['mww@macports.org', 'pmq@macports.org']       
        )
        
        p3 = package.Package(
            name                   = 'python25',
            version                = '2.5',
            directory              = 'lang/python25',    
        )
        
        array_keys = [key for key in package.PackageKey if key.endswith('s')]
        for key in array_keys:
            self.assertEquals(p3[key], [])
        self.assertEquals(p3[PackageKey.DESCRIPTION], '')
        
        
    def testMissingPackageName(self):
        self.assertRaises(package.InvalidPackage, package.Package, 
            {   PackageKey.VERSION   : '2.4', 
                PackageKey.DIRECTORY : 'lang/python24',
                PackageKey.HOMEPAGE  : 'http://www.python.org/'})
                
    def testMissingPackageVersion(self):
        self.assertRaises(package.InvalidPackage, package.Package, 
            {   PackageKey.NAME      : 'python',
                PackageKey.DIRECTORY : 'lang/python24',
                PackageKey.HOMEPAGE  : 'http://www.python.org/'})
                
    def testMissingPackageDir(self):    
        self.assertRaises(package.InvalidPackage, package.Package, 
            {   PackageKey.NAME      : 'python',
                PackageKey.VERSION   : '2.4', 
                PackageKey.HOMEPAGE  : 'http://www.python.org/'})

            
if __name__ == '__main__':
    unittest.main()